package xin.yogee.ms.module.oa.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import xin.yogee.ms.common.BaseController;
import xin.yogee.ms.module.oa.entity.Product;
import xin.yogee.ms.module.oa.service.IProductService;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 商品 前端控制器
 * </p>
 *
 * @author jackieQ
 * @since 2020-02-27
 */
@RestController
@RequestMapping("/oa/product")
public class ProductController extends BaseController {
    @Autowired
    private IProductService productService;


    @RequestMapping(value = "/all")
    public String all(@RequestBody JSONObject json){
        List<Product> productList = productService.list();
        return successJson("success",productList,"获取成功");
    }

    @RequestMapping(value = "/get")
    public String get(@RequestBody JSONObject json){
        String id = json.getString("id");
        Product product = productService.getById(id);
        return successJson("success",product,"获取成功");
    }

    @RequestMapping(value = "/list")
    public String list(Product product, HttpServletRequest request){
        String current = request.getParameter("currentPage") == null ? "1" : request.getParameter("currentPage");
        String pageSize = request.getParameter("pageSize") == null ? "10" : request.getParameter("pageSize");
        IPage<Product> productList = productService.getList(new Page<Product>(Long.valueOf(current),Long.valueOf(pageSize)));
        return successJson("success",productList,"获取成功");
    }

    @RequestMapping(value = "/store")
    @ResponseBody
    public String store(@RequestBody JSONObject json, Product product){
        String id = json.getString("id");
        String productName = json.getString("productName");
        String unit = json.getString("unit");
        String standards = json.getString("standards");
        String category = json.getString("category");
        product.setId(id);
        product.setProductName(productName);
        product.setUnit(unit);
        product.setStandards(standards);
        product.setCategory(category);
        productService.saveOrUpdate(product);
        return successJson("success", new HashMap(0), "创建成功");
    }

    @RequestMapping(value = "/del")
    public String del(@RequestBody JSONObject json){
        String id = json.getString("productId");
        productService.removeById(id);
        return successJson("success","","删除成功");
    }
}
